Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Fast and simple xml generator. Supports attributes, CDATA, etc. Includes tests and examples.
The 'xml' npm package is a utility for working with XML data in Node.js. It provides functionalities for parsing XML strings and converting JavaScript objects into XML format. This package is useful for applications that need to interact with XML data or APIs that use XML for data exchange.
Parsing XML
This feature allows the parsing of XML strings into JavaScript objects, enabling easy access and manipulation of the data contained within the XML.
const xml = require('xml');
const xmlString = '<root><child>value</child></root>';
const parsedXml = xml.parse(xmlString);
console.log(parsedXml);
Building XML
This feature enables the conversion of JavaScript objects into XML strings, which is useful for creating XML data dynamically from an application.
const xml = require('xml');
const jsObject = { root: { child: 'value' } };
const xmlString = xml(jsObject);
console.log(xmlString);
xml2js is a comprehensive XML parsing library that converts XML to JavaScript objects and vice versa. It offers more advanced options for customization and handling of XML attributes and text nodes compared to 'xml', making it suitable for more complex XML manipulation tasks.
fast-xml-parser is a high-performance XML parser that can validate and parse XML data quickly. It supports both conversion from XML to JSON and JSON to XML. It is known for its speed and efficiency, especially in handling large XML files, which might give it an edge over 'xml' for performance-critical applications.
Fast and simple Javascript-based XML generator/builder for Node projects.
$ npm install xml
xml(xmlObject, options)
Returns a XML
string.
var xml = require('xml');
var xmlString = xml(xmlObject, options);
xmlObject
xmlObject
is a normal JavaScript Object/JSON object that defines the data for the XML string.
Keys will become tag names.
Values can be an array of xmlObjects
or a value such as a string
or number
.
xml({a: 1}) === '<a>1</a>'
xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'
There are two special keys:
_attr
Set attributes using a hash of key/value pairs.
xml({a: [{ _attr: { attributes: 'are fun', too: '!' }}, 1]}) === '<a attributes="are fun" too="!">1</a>'
_cdata
Value of _cdata
is wrapped in xml ![CDATA[]]
so the data does not need to be escaped.
xml({a: { _cdata: "i'm not escaped: <xml>!"}}) === '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'
Mixed together:
xml({a: { _attr: { attr:'hi'}, _cdata: "I'm not escaped" }}) === '<a attr="hi"><![CDATA[I\'m not escaped]]></a>'
options
indent
optional string What to use as a tab. Defaults to no tabs (compressed).
For example you can use '\t'
for tab character, or ' '
for two-space tabs.
stream
Return the result as a stream
.
Stream Example
var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
var stream = xml({ toys: elem }, { stream: true });
stream.on('data', function (chunk) {console.log("data:", chunk)});
elem.push({ toy: 'Transformers' });
elem.push({ toy: 'GI Joe' });
elem.push({ toy: [{name:'He-man'}] });
elem.close();
/*
result:
data: <toys decade="80s" locale="US">
data: <toy>Transformers</toy>
data: <toy>GI Joe</toy>
data: <toy>
<name>He-man</name>
</toy>
data: </toys>
*/
Declaration
optional Add default xml declaration as first node.
options are:
Declaration Example
xml([ { a: 'test' }], { declaration: true })
//result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>'
xml([ { a: 'test' }], { declaration: { standalone: 'yes', encoding: 'UTF-16' }})
//result: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'
Simple Example
var example1 = [ { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } ];
console.log(XML(example1));
//<url>http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower</url>
Example with attributes
var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } } } ];
console.log(XML(example2));
//result: <url hostname="www.google.com" path="/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower"/>
Example with array of same-named elements and nice formatting
var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
console.log(XML(example3));
//result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>
console.log(XML(example3, true));
/*
result:
<toys>
<toy>Transformers</toy>
<toy>GI Joe</toy>
<toy>He-man</toy>
</toys>
*/
More complex example
var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ];
console.log(XML(example4, true));
/*
result:
<toys decade="80s" locale="US">
<toy>Transformers</toy>
<toy>GI Joe</toy>
<toy>He-man</toy>
</toys>
*/
Even more complex example
var example5 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: [ { _attr: { knowing: 'half the battle' } }, 'GI Joe'] }, { toy: [ { name: 'He-man' }, { description: { _cdata: '<strong>Master of the Universe!</strong>'} } ] } ] } ];
console.log(XML(example5, true));
/*
result:
<toys decade="80s" locale="US">
<toy>Transformers</toy>
<toy knowing="half the battle">
GI Joe
</toy>
<toy>
<name>He-man</name>
<description><![CDATA[<strong>Master of the Universe!</strong>]]></description>
</toy>
</toys>
*/
Tests included use AVA. Use npm test
to run the tests.
$ npm test
There are examples in the examples directory.
Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.
FAQs
Fast and simple xml generator. Supports attributes, CDATA, etc. Includes tests and examples.
The npm package xml receives a total of 4,880,586 weekly downloads. As such, xml popularity was classified as popular.
We found that xml demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.